package logic

import (
	"book-hub/bookrpc/bookrpc"
	"book-hub/bookrpc/internal/svc"
	"book-hub/models"
	"context"
	"fmt"
	"strings"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *bookrpc.CommentAddRequest) (*bookrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line
	if strings.Contains(in.Content, "垃圾") {
		return nil, fmt.Errorf("包含敏感词")
	}
	comment := models.Comment{
		Content: in.Content,
		UserId:  int(in.UserId),
		NovelId: int(in.NovelId),
	}
	models.DB.Create(&comment)
	return &bookrpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
